package org.hawk.gwt.ppc.reflect.lang;

import java.util.HashSet;
import java.util.Set;

public final class LangClassLoader extends ClassLoaderDelegate {
	public static final String LANG_PACKAGE = "java.lang";

	private static final Class<?>[] LANG_CLASSES = new Class<?>[]{
			Appendable.class,
			ArithmeticException.class,
			ArrayIndexOutOfBoundsException.class,
			ArrayStoreException.class,
			AssertionError.class,
			Boolean.class,
			Byte.class,
			Character.class,
			CharSequence.class,
			Class.class,
			ClassCastException.class,
			ClassNotFoundException.class,
			Cloneable.class,
			Comparable.class,
			Double.class,
			Enum.class,
			Error.class,
			Exception.class,
			Float.class,
			IllegalAccessError.class,
			IllegalAccessException.class,
			IllegalArgumentException.class,
			IllegalStateException.class,
			IndexOutOfBoundsException.class,
			InstantiationException.class,
			Integer.class,
			InternalError.class,
			Iterable.class,
			Long.class,
			Math.class,
			NegativeArraySizeException.class,
			NoSuchFieldException.class,
			NoSuchMethodException.class,
			NullPointerException.class,
			Number.class,
			NumberFormatException.class,
			Object.class,
			Package.class,
			Runnable.class,
			RuntimeException.class,
			Short.class,
			StackTraceElement.class,
			String.class,
			StringBuilder.class,
			StringIndexOutOfBoundsException.class,
			Throwable.class,
			UnsupportedOperationException.class
		};
	
	private static Set<String> classNames;
	
	public static boolean isLangClass(String name){
		if(classNames==null){
			classNames = new HashSet<String>();
			for(Class<?> cls : LANG_CLASSES){
				classNames.add(getSimpleName(cls));
			}
		}
		return classNames.contains(name);
	}

	private static String getSimpleName(Class<?> cls) {
		String name = cls.getName();
		int i = name.lastIndexOf('.');
		if(i<0)return name;
		return name.substring(i+1);
	}

	@Override
	protected ClassDelegate<?>[] getClasses() {
		return new ClassDelegate<?>[0];
	}

	@Override
	protected String getPackage() {
		return LANG_PACKAGE;
	}
	
	@Override
	protected Class<?>[] getNotReflectableClasses() {
		return LANG_CLASSES;
	}
}